// 创建axios实例
import axios from 'axios';

const instance = axios.create({
  baseURL: 'https://api.bilibili.com', // API基础地址
  timeout: 10000, // 请求超时时间
});

// 请求拦截器
instance.interceptors.request.use(
  (config) => {
    // 设置请求头
    config.headers['accept'] = 'application/json, text/plain, */*';
    config.headers['accept-language'] = 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6';
    config.headers['priority'] = 'u=1, i';

    // 添加随机参数避免缓存
    const randomParam = `cache_buster=${Date.now()}`;
    if (config.url) {
      config.url += (config.url.includes('?') ? '&' : '?') + randomParam;
    }

    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
instance.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 获取课程列表
export const fetchCourseList = async (params: { [key: string]: any }) => {
  try {
    const response = await instance.get('/pugv/app/web/seasonSeek', { params });
    return response.data;
  } catch (error) {
    console.error('Error fetching course list:', error);
    throw error;
  }
};